package com.TeacherInfoServer.controller;

import com.TeacherInfoServer.common.Result;
import com.TeacherInfoServer.pojo.DTO.DepartmentDTO;
import com.TeacherInfoServer.listener.DepartmentListener;
import com.TeacherInfoServer.listener.ExcelRespProp;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.DepartmentSearchDTO;
import com.TeacherInfoServer.pojo.excelImport.DepartmentExcel;
import com.TeacherInfoServer.common.service.DepartmentService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 教研室信息控制器

 */
@Slf4j
@RestController
@RequestMapping("/s1/department")
@Tag(name = "教研室信息控制器",description = "用于对教研室信息的控制")
public class DepartmentController {
    @Autowired
    public DepartmentService departmentService;
    @Autowired
    public DepartmentListener departmentListener;

    @Operation(summary = "获取教研室信息列表",description = "分页获取教研室信息列表")
    @GetMapping("/getList")
    public Result<Map<String,Object>> getAllList(@RequestParam("pages") @Parameter(description = "当前页数") Integer pages,
                                                 @RequestParam("size") @Parameter(description = "当前页数显示信息的数量") Integer size){
        return Result.success("获取教研室列表成功",departmentService.getDepartmentList(pages,size));
    }

    @Operation(summary = "模糊查询获取教研室信息列表",description = "模糊查询分页获取教研室信息列表")
    @PostMapping("/searchList")
    public Result<Map<String,Object>> searchList(@RequestBody DepartmentSearchDTO departmentSearchDTO){
        return Result.success("获取教研室列表成功",departmentService.searchDepartmentList(departmentSearchDTO));
    }

    @Operation(summary = "添加单个教研室信息",description = "添加单个教研室信息")
    @PostMapping("/addDepartment")
    public Result<?> addTeacher(@RequestParam("name") @Parameter(description = "教研室名称") String name,
                                @RequestParam("leaderName") @Parameter(description = "现任专业负责人姓名") String leaderName,
                                @RequestParam("directorName") @Parameter(description = "现任专业教研室主任姓名") String directorName){
         departmentService.addDepartment(name,leaderName,directorName);
         return Result.success("添加教研室信息成功");
    }

    @Operation(summary = "更新单个教研室信息",description = "更新单个教研室信息")
    @PutMapping("/updateDepartment")
    public Result<?> updateTeacher(@RequestBody DepartmentDTO departmentDTO){
        departmentService.updateDepartment(departmentDTO);
        return Result.success("添加教研室信息成功");

    }

    @Operation(summary = "根据id批量删除教研室信息",description = "根据id批量删除教研室信息")
    @DeleteMapping("/deleteDepartment")
    public Result<?> deleteDepartment(@RequestParam("ids") @Parameter(description = "选择删除的教研室列表") List<Long> ids){
        boolean isDelete=departmentService.deleteDepartment(ids);
        if (isDelete) {
            return Result.success("删除成功");
        }
        else {
            return Result.fail("没有相应教研室，删除失败");
        }
    }

    @Operation(summary = "Excel批量导入教研室信息",description = "Excel批量导入教研室信息")
    @PostMapping("/addByBatch")
    @Transactional
    public Result<Map<String,Integer>> addBatch(@RequestParam("file")@Parameter(description = "添加Excel文件") MultipartFile file) throws IOException {
        departmentListener.initList();
        EasyExcel.read(file.getInputStream(), DepartmentExcel.class,departmentListener).sheet().doRead();
        Map<String,Integer> map = new HashMap<>();
        map.put("record",departmentListener.getRecord());
        return Result.success(200,"批量导入成功",map);
    }

    @Operation(summary = "Excel导出教研室信息",description = "Excel导出教研室信息")
    @SneakyThrows(IOException.class)
    @GetMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response){
        ExcelRespProp.setExcelRespProp(response, "教研室信息列表");
        List<DepartmentDTO> departmentDTOS = departmentService.getDepartmentDTOS();
        EasyExcel.write(response.getOutputStream())
                .head(DepartmentDTO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("教研室信息列表")
                .doWrite(departmentDTOS);

    }

    @Operation(summary = "获取教研室名称列表",description = "获取所有教研室名称列表")
    @GetMapping("/getDepartmentNameList")
    public Result<Map<String,Object>> getDepartmentNameList(@RequestParam(required = false) String departmentName){
        return Result.success("获取教研室名称列表成功",departmentService.getDepartmentNameList(departmentName));
    }
}
